package com.foreverwin.mes.production.controller;


import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.production.model.StationCertification;
import com.foreverwin.mes.production.service.StationCertificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

/**
 * @author syngna
 * @since 2018-11-29
 */
@RestController
@RequestMapping("/stationCertifications")
public class StationCertificationController {
    private final Logger logger = LoggerFactory.getLogger(StationCertificationController.class);

    @Autowired
    public StationCertificationService stationCertificationService;

    /**
     * 根据id查询
     *
     * @param id 主键
     * @return
     */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getStationCertificationById(@PathVariable String id) {
        StationCertification result = null;
        try {
            result = stationCertificationService.selectById(id);
        } catch (Exception e) {
            logger.error("getStationCertificationById -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getStationCertificationList() {
        List<StationCertification> result = new ArrayList<StationCertification>();
        try {
            result = stationCertificationService.selectList();
        } catch (Exception e) {
            logger.error("getStationCertificationList -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage 分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getStationCertificationPageList")
    public AjaxResult getStationCertificationPageList(FrontPage<StationCertification> frontPage) {
        List<StationCertification> result = new ArrayList<StationCertification>();
        try {
            result = stationCertificationService.selectPage(frontPage).getRecords();
        } catch (Exception e) {
            logger.error("getStationCertificationPageList -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     *
     * @param stationCertification 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult stationCertificationSave(StationCertification stationCertification) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count = 0;
        try {
            count = stationCertificationService.insertOrUpdate(stationCertification) ? 1 : 0;
        } catch (Exception e) {
            logger.error("stationCertificationSave -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(count == 0 ? null : stationCertification);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public AjaxResult stationCertificationDelete(@PathParam("id") String id) {
        int count = 0;
        try {
            count = stationCertificationService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("stationCertificationDelete -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     *
     * @param ids 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult deleteBatchIds(List<String> ids) {
        int count = 0;
        try {
            count = stationCertificationService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("stationCertificationBatchDelete -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("count: " + count);
    }
}